<template>
  <div class="page-content">
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-swipe class="m-swiper" :autoplay="2000">
        <van-swipe-item v-for="item in bannerList" :key="item">
          <img v-lazy="apihost + item.images" @click="gotoUrl(item)" />
        </van-swipe-item>
      </van-swipe>
      <van-notice-bar :text="notice" left-icon="volume-o" color="rgb(253, 57, 57)" />
      <div class="m-menu backfff flex flex-jc">
        <div class="flex flex-wrap">
          <van-row :gutter="16">
            <van-col span="6" class="navLi" v-for="(item, index) in menuList" :key="index">
              <router-link :to="item.path" v-if="item.path ? true : false">
                <img v-lazy="item.icon" alt="菜单选项" />
                <div>{{ item.name }}</div>
              </router-link>
              <template v-else>
                <img v-lazy="item.icon" alt="菜单选项" @click="sign(item)" />
                <div>{{ item.name }}</div>
              </template>
            </van-col>
          </van-row>
        </div>
      </div>
      <div style="background:#fff;" v-if="myVideoSrc">
        <div class="videoContent" ref="videoContent">
          <video id="myVideo" class="video-js">
            <source :src="myVideoSrc" type="video/mp4" />
          </video>
        </div>
      </div>

      <home-list ref="list" @ok="onRefreshed"></home-list>
      <common-footer></common-footer>
      <div class="appbottom" v-if="appIsShow">
        <div class="applogo">
          <img v-lazy="apihost + appList.webico" alt />
        </div>
        <div class="appinfo">
          <p>{{ appList.title }}</p>
          <p>点击下载App</p>
        </div>
        <div class="appdownload">
          <div class="btn">
            <a :href="appList.appdown">立即下载</a>
          </div>
          <span @click="appShow">X</span>
        </div>
      </div>
    </van-pull-refresh>
  </div>
</template>

<script>
import { commonFooter, homeList } from "common";
import { usignApi } from "@/api/user";
import {
  ACCESS_TOKEN,
  WEB_QQ,
  WEB_TEL,
  COPY_RIGHT,
  API_HOST
} from "@/store/mutation-types";
import { indexInitApi, newsApi, openconfApi } from "@/api";
import { mapMutations } from "vuex";
import { fstat } from "fs";
export default {
  data() {
    return {
      isLoading: false,
      appIsShow: true,
      transition: "",
      transform: "",
      title: "首页",
      loading: false,
      applogo: "",
      bannerList: [],
      goodsList: [],
      scollShow: true,
      appList: {
        weblogo: "",
        webico: "",
        title: "",
        appdown: ""
      },
      myVideoSrc: "",
      notice: "",
      menuList: [
        {
          name: "平台公告",
          icon: require("../../assets/imgs/menu_00.png")
          // path: "/aboutus"
        },
        {
          name: "我要签到",
          icon: require("../../assets/imgs/menu_01.png")
        },
        {
          path: "/user/recharge",
          name: "我要充值",
          icon: require("../../assets/imgs/menu_02.png")
        },
        {
          path: "/user/withdrawal",
          name: "我要提现",
          icon: require("../../assets/imgs/menu_03.png")
        },
        {
          path: "/user/invitation",
          name: "邀请有礼",
          icon: require("../../assets/imgs/menu_04.png")
        },
        {
          path: "/user/investment",
          name: "我的投资",
          icon: require("../../assets/imgs/menu_05.png")
        },
        {
          path: "/home/calculator",
          name: "收益计算",
          icon: require("../../assets/imgs/menu_06.png")
        },
        {
          path: "/product",
          name: "投资项目",
          icon: require("../../assets/imgs/menu_07.png")
        }
      ],
      marqueeTextWidth: 0,
      leadingTimeout: null,
      marqueeTimeOut: null,
      subordinateTimeout: null
    };
  },
  beforeDestroy() {
    this.leadingTimeout = null;
    this.marqueeTimeOut = null;
    this.subordinateTimeout = null;
    this.myVideoSrc = "";
    this.myPlayer.dispose();
  },
  methods: {
    gotoUrl(item) {
      if (item.url) {
        window.open(item.url, "_self");
      }
    },
    async onRefresh() {
      await this.getInit();
      this.$refs.list.onRefresh();
    },
    onRefreshed() {
      this.isLoading = false;
    },
    getConf() {
      this.$store.dispatch("GET_CONF").then(res => {
        this.notice = res.data.notice || "";
        this.appList = res.data;
        this.SET_HOMELOGO(res.data.weblogo);
        this.$ls.set(WEB_TEL, res.data.webtel);
        this.$ls.set(WEB_QQ, res.data.webqq);
        this.$ls.set(COPY_RIGHT, res.data.copyright);
        this.$store.commit("SET_CHARTURL", res.data.onlinechat);
        this.apihost = res.data.apihost;
        if (res.data.videourl) {
          this.myVideoSrc = res.data.videourl;
          this.$nextTick(() => {
            this.initVideo(res.data);
          });
        }
      });
    },
    getInit() {
      // 获取首页信息
      indexInitApi().then(res => {
        if (res.code == 200) {
          this.bannerList = res.data.banner;
          // this.goodsList = res.data.goods;
        }
      });
      return;
    },
    appShow() {
      // 是否显示下载
      this.appIsShow = false;
    },
    sign(item) {
      if (item.name == "平台公告") {
        newsApi({ title: "平台公告" }).then(res => {
          console.log(res);
          if (res.code == 200) {
            if (res.data.length == 0) {
              this.$router.push({ path: "/aboutus" });
            } else {
              this.$router.push({ path: `/aboutus/list/${res.data[0].token}` });
            }
          }
        });
      } else if (item.name == "我要签到") {
        let _this = this;
        if (!this.$ls.get(ACCESS_TOKEN)) {
          this.$router.push({
            path: "/login",
            query: { redirect: this.$route.path }
          });
          return;
        }
        usignApi().then(res => {
          _this.$toast({
            message: res.msg,
            duration: 1500
          });
        });
      }
    },
    marqueeRun() {
      this.marqueeTextWidth = this.$refs.marquee.offsetWidth;
      if (this.marqueeTextWidth > this.$refs.noticescroll.offsetWidth) {
        this.leadingAction(); // 跑起来
      } else {
        this.scollShow = false;
      }
    },
    leadingAction() {
      this.leadingTimeout = setTimeout(() => {
        this.marqueeAction();
      }, 1000);
    },
    marqueeAction() {
      // 跑动的 判断是不是定速  因为这里不需要实时的改变 所以不需要再watch 里面监控 ifSpeed 的变化 等定时器停止之后去监控

      this.transform = `translateX(${-this.marqueeTextWidth}px)`;
      this.transition = `transform 6000ms linear`;
      // none 0s ease 0s
      this.marqueeTimeOut = setTimeout(() => {
        this.subordinateAction();
      }, 6000);
    },
    subordinateAction() {
      // 轮播结束的事件
      this.subordinateTimeout = setTimeout(() => {
        // 因为loop会从false 变成true 要重新启动
        // 不是轮播直接停止
        this.transform = `translateX(0px)`;
        this.transition = `none 0s ease 0s`;
        this.leadingAction();
      }, this.subordinate);
    },
    ...mapMutations(["SET_HOMELOGO"]),
    initVideo(item) {
      let width = this.$refs.videoContent.offsetWidth;
      //初始化视频方法
      this.myPlayer = this.$video("myVideo", {
        //确定播放器是否具有用户可以与之交互的控件。没有控件，启动视频播放的唯一方法是使用autoplay属性或通过Player API。
        controls: true,
        //自动播放属性,muted:静音播放
        // autoplay: "muted",
        //建议浏览器是否应在<video>加载元素后立即开始下载视频数据。
        preload: "auto",
        poster: item.apihost + item.videocover,
        //设置视频播放器的显示宽度（以像素为单位）
        width: width + "px",
        //设置视频播放器的显示高度（以像素为单位）
        height: width * 0.66 + "px"
      });
    }
  },
  components: {
    commonFooter,
    homeList
  },
  mounted() {
    this.getConf();
    this.getInit();
  }

  // computed: {
  //   apihost() {
  //     return this.$ls.get(API_HOST);
  //   }
  // }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
@import "~styles/index.less";
@import "~styles/variable.less";
/deep/ .video-js {
  display: inline-block;
}
.videoContent {
  width: 95%;
  margin: 0 auto;
  background: #fff;
  text-align: center;
}
.page-content {
  .m-swiper {
    width: 100%;
    box-sizing: border-box;
    // .h(400);
    text-align: center;

    img {
      width: 100%;
      height: 5rem;
      vertical-align: top;
    }
  }
  .m-notice {
    display: flex;
    align-items: center;
    .padding(20, 30, 30, 20);
    .notice-icon {
      .mr(12);
      .w(96);
      .h(48);
    }
    .m-notice-scroll {
      width: 100%;
      overflow: hidden;
      white-space: nowrap;
      .h(64);
      .lh(64);
      .fs(32);
    }
  }
  .m-menu {
    .padding(20, 30, 10, 30);
    .bb(1, #eee);
    .menu-item {
      .tc();
      a {
        color: #2c3e50;
      }
      .w(172.5);
      .mb(20);
      img {
        .w(120);
        .h(120);
        .mb(8);
      }
    }
  }
}
.navLi {
  text-align: center;
  font-size: 16px;
  padding: 0.2rem 0;
  img {
    width: 1.2rem;
    margin-bottom: 0.2rem;
  }
}
.appbottom {
  display: flex;
  align-items: center;
  position: fixed;
  width: 100%;
  max-width: 600px;
  box-sizing: border-box;
  bottom: 50px;
  padding: 0.1rem;
  background: rgba(0, 0, 0, 0.3);
  .applogo {
    width: 1rem;
    img {
      width: 100%;
      vertical-align: top;
    }
  }
  .appinfo {
    color: #fff;
    flex: 1;
    padding-left: 0.4rem;
    font: 0.35rem/1.5 "Microsoft YaHei";
  }
  .appdownload {
    width: 3rem;
    .btn {
      width: 1.8rem;
      text-align: center;
      a {
        font: 0.35rem/2 "Microsoft YaHei";
        color: #fff;
      }

      background: @base-color;
      border-radius: 0.5rem;
      float: left;
    }

    span {
      float: right;
      font: 0.35rem/2 "Microsoft YaHei";
      color: #fff;
      padding-right: 0.4rem;
    }
  }
}

/deep/ .van-notice-bar {
  padding: 6px 12px;
  font-size: 16px;
}
.navLi a {
  color: #333;
  font-size: 16px;
}
</style>
